package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.AreaDao;
import com.pojo.Area;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AreaService extends ServiceImpl<AreaDao, Area> {
    @Autowired
    private AreaDao areaDao;

    /**
     * 分页查询地区
     * @param name  关键字
     * @param page  页面
     * @param limit  条数
     * @return
     */
    public IPage selectAreas(int page, int limit, String name,String country_id) {
        Page p = new Page(page, limit);
        QueryWrapper<Area> query = new QueryWrapper();
        if(StringUtil.isNotNull(name)) {
            query.and(wrapper->wrapper.like("area_zh_name" , name).or().like("area_en_name",name));
        }
        if(StringUtil.isNotNull(country_id)){
            query.eq("country_id",country_id);
        }
        query.orderByDesc("country_id");
        IPage iPage = areaDao.selectPage(p, query);
        return iPage;
    }
}
